var imgloader = null;
window.addEvent('domready', function(){
	imgloader = new ImgLoader();
});

var ImgLoader = new Class({
	initialize: function(){
		this.container = null;
		this.paths = [];
	},

	load: function(html){
		this.container = new Element('div', {
			'id' : 'img_loader',
			'html' : html,
			'styles' : {
				'display' : 'none'
			}
		}).inject($$('body')[0]);

		var imgs = this.container.getElements('img');
		imgs.each(function(img){
			this.paths.push(img.get('src'));
		}.bind(this));
	},

	start: function(){
		if(this.container == null){
			console.log('Error : no html to load');
			return;
		}

		if (this.paths.length > 0){
			new Asset.images(this.paths, {
				onProgress: function(){
					this.progress();
				}.bind(this),
				onComplete: function(){
					this.complete();
				}.bind(this)
			});
		} else
			this.complete();
	},

	complete: function(){
		window.fireEvent('imgloader_complete');
		this.paths = [];
		this.container.destroy();
	},

	progress: function(){
		window.fireEvent('imgloader_progress');
	}
});